#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: editor.py
# Author: Tomás Vírseda
# License: GPL v3
# Description:  Edit resources properties

from rdflib import Literal, URIRef

from vazaarlib.namespaces import *

class ResourceEditor():
    def __init__(self, gui):
        self.gui = gui
        self.id = None
        self.old_name = None
        self.new_name = None
        self.old_tags = None
        self.new_tags = None
        self.gui.frontend['rpe-name'] = self.gui.glade.widgets.get_widget("etyEditResName")
        self.gui.frontend['rpe-name'].connect('activate', self.update_all)
        self.gui.frontend['rpe-ltype'] = self.gui.glade.widgets.get_widget("lblEditResType")
        self.gui.frontend['sh-editres'] = self.gui.glade.widgets.get_widget("cntEditResource")
        self.gui.frontend['sh-btnnoedit'] = self.gui.glade.widgets.get_widget("btnCancelEdit")
        self.gui.frontend['sh-btnnoedit'].connect('clicked', self.hide_dialog)
        self.gui.frontend['rpe-tags'] = self.gui.glade.widgets.get_widget("etyResTags")
        self.gui.frontend['rpe-tags'].connect('activate', self.update_all)

    def set_data(self):
        try:
            self.gui.frontend['rpe-name'].set_text(self.gui.ask.get_property(self.id, NIE['title']))
            tags = self.gui.ask.tags(self.id)
            self.gui.frontend['rpe-tags'].set_text((', ').join(tags))
            self.old_name = self.gui.frontend['rpe-name'].get_text()
            self.old_tags = self.gui.frontend['rpe-tags'].get_text()
        except:
            self.gui.actions.update_statusbar("It seems like this resource doesn't exist anymore...")

    def update_all(self, *params):
        self.update_name()
        self.update_tags()
        self.gui.actions.update_gui()
        self.hide_dialog()

    def update_name(self, *params):
        new_name = self.gui.frontend['rpe-name'].get_text()
        if self.old_name == new_name:
            return None
        self.old_name = self.new_name
        self.new_name = new_name
        try:
            #print "editor", self.old_name, self.new_name
            self.gui.app.vstore.update_resource_property(URIRef(self.id), NIE['title'], Literal(self.old_name), Literal(self.new_name))

        except Exception, error:
            pass



    def update_tags(self, *params):
        new_tags = self.gui.frontend['rpe-tags'].get_text()

        if self.old_tags == new_tags:
            # nothing changed from last time
            return None

        self.new_tags = new_tags
        self.old_tags = self.new_tags

        target = []
        s = URIRef(self.id)
        p = NAO['hasTag']
        try:
            # get old tags and remove them
            old_tags = self.gui.ask.get_tags(self.id)
            for tag in old_tags:
                target.append((s, p, Literal(tag)))
            self.gui.app.vstore.delete_triples(target)
        except Exception, error:
            pass #print error

        try:
            # add new tags
            target = []
            for label in self.new_tags.split(','):
                tag = label.split()
                tag = str(('').join(tag))
                target.append((s, p, Literal(tag)))
            #print target
            self.gui.app.vstore.add_triples(target)

        except Exception, error:
            pass #print "update_tags", error



    def update_id(self, id):
        self.id = id
        self.set_data()

    def show_dialog(self):
        self.gui.frontend['sh-editres'].set_no_show_all(False)
        self.gui.frontend['sh-editres'].show_all()
        self.gui.frontend['content'].set_sensitive(False)
        self.gui.frontend['actionbar'].set_sensitive(False)

    def hide_dialog(self, *args):
        self.gui.frontend['sh-editres'].hide_all()
        self.gui.frontend['content'].set_sensitive(True)
        self.gui.frontend['actionbar'].set_sensitive(True)
        self.id = None
